<%@ page language="java" import="com.engine.common.util.ServiceUtil" pageEncoding="UTF-8" %>
<%@ page import="com.engine.workflow.constant.PAResponseCode" %>
<%@ page import="com.engine.workflow.entity.publicApi.PAResponseEntity" %>
<%@ page import="com.engine.workflow.entity.publicApi.ReqOperateRequestEntity" %>
<%@ page import="com.engine.workflow.entity.publicApi.WorkflowDetailTableInfoEntity" %>
<%@ page import="com.engine.workflow.entity.requestForm.ReqFlowFailMsgEntity" %>
<%@ page import="com.engine.workflow.publicApi.WorkflowRequestOperatePA" %>
<%@ page import="com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl" %>
<%@ page import="static weaver.common.DateUtil.getDateTime" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.workflow.webservices.WorkflowRequestTableField" %>
<%@ page import="weaver.workflow.webservices.WorkflowRequestTableRecord" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%--流程退回调用QMS接口--%>
<%

    String formid = Util.null2String(request.getParameter("formid"));
    String ids = Util.null2String(request.getParameter("ids"));
    try {
        RecordSet rs = new RecordSet();
        User user = HrmUserVarify.getUser(request, response);
        out.print(">>>>>测试接口>>>>>formid=" + formid + ",ids=" + ids + "<br/>");
        rs.executeQuery("select * from uf_zjjhhzb where id in (" + ids + ") and nvl(sfcffklc,1) != 0");
        while (rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            String jbr = Util.null2String(rs.getString("jbr"));
            String bm = Util.null2String(rs.getString("bm"));
            String jyzfnr = Util.null2String(rs.getString("jyzfnr"));
            String cwfhjeyb = Util.null2String(rs.getString("cwfhjeyb"));
            String ksbm = Util.null2String(rs.getString("ksbm"));
            String ksmc = Util.null2String(rs.getString("ksmc"));
            String lxhdh = Util.null2String(rs.getString("lxhdh"));
            String khyxdh = Util.null2String(rs.getString("khyxdh"));
            String yxzhdh = Util.null2String(rs.getString("yxzhdh"));
            String lxhcd = Util.null2String(rs.getString("lxhcd"));
            String khyxcd = Util.null2String(rs.getString("khyxcd"));
            String yxzhcd = Util.null2String(rs.getString("yxzhcd"));
            String zjmxxmwb = Util.null2String(rs.getString("zjmxxmwb"));
            String jhid = Util.null2String(rs.getString("jhid"));
            String lzdjh = Util.null2String(rs.getString("lzdjh"));
            String bz = Util.null2String(rs.getString("bz"));
            String gsdm = Util.null2String(rs.getString("gsdm"));
            String ssgs = "123";
            String wlkmbm = Util.null2String(rs.getString("wlkmbm"));
            String wlkmbs = Util.null2String(rs.getString("wlkmbs"));
            String ysbm = Util.null2String(rs.getString("ysbm"));
            String ysxm = Util.null2String(rs.getString("ysxm"));
            String ydzqysfyeyb = Util.null2String(rs.getString("ydzqysfyeyb"));
            String cwfhjebb = Util.null2String(rs.getString("cwfhjebb"));
            String yfpzhjnd = Util.null2String(rs.getString("yfpzhjnd"));
            String yfpzxxmbh = Util.null2String(rs.getString("yfpzxxmbh"));
            String zjmxxm = Util.null2String(rs.getString("zjmxxm"));
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put("jbr", jbr);
            requestMainMap.put("jbrwb", "test");
            requestMainMap.put("bm", bm);
            requestMainMap.put("gh", "0000080");
            requestMainMap.put("sqrq", "2024-01-01");
            requestMainMap.put("yksxfl", "0");
            requestMainMap.put("qwzfrq", "2024-01-01");
            requestMainMap.put("yksxsm", jyzfnr);
            requestMainMap.put("zffs", "1");
            requestMainMap.put("hkfyqtxfksxbzdfyxxjljd", jyzfnr);
            requestMainMap.put("bcqkjehjxx", cwfhjeyb);
            requestMainMap.put("skdwxz", "1");
            requestMainMap.put("gsdm", gsdm);
            requestMainMap.put("ssgs", ssgs);
            requestMainMap.put("ksbm", ksbm);
            requestMainMap.put("lxh", lxhdh);
            requestMainMap.put("khx", khyxdh);
            requestMainMap.put("zh", yxzhdh);
            requestMainMap.put("lxhcd", lxhcd);
            requestMainMap.put("khxcd", khyxcd);
            requestMainMap.put("yxzhcd", yxzhcd);
            requestMainMap.put("pldb", "0");
            List<Map<String, Object>> detailList = new ArrayList<>();
            Map<String, Object> detailMap = new HashMap<>();
            detailMap.put("tablename","formtable_main_"+formid+"_dt1");
            List<Map<String, String>> fieldList = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            map.put("zjjhxh", id);
            map.put("ly", "EC");
            map.put("zjmxxmwb", zjmxxmwb);
            map.put("zjjhxhwb", jhid);
            map.put("skdwdm", ksbm);
            map.put("skdw", ksmc);
            map.put("lzdjh", lzdjh);
            map.put("bz", bz);
            map.put("gsdm", gsdm);
            map.put("wlkmbh", wlkmbm);
            map.put("wlkm", wlkmbs);
            map.put("yydmxjlm", ysbm);
            map.put("xjlkm", ysxm);
            map.put("ydzqysfyeyb", ydzqysfyeyb);
            map.put("jeyb", cwfhjeyb);
            map.put("bcqkjeyb", cwfhjeyb);
            map.put("hl", "1");
            map.put("bcqkjebb", cwfhjebb);
            map.put("pldb", "0");
            map.put("yfpzhjnd", yfpzhjnd);
            map.put("yfpzxxmbh", yfpzxxmbh);
            map.put("zjmxxm", zjmxxm);
            map.put("jyzfnr", jyzfnr);
            fieldList.add(map);
            detailMap.put("tablefield",fieldList);
            detailList.add(detailMap);
            Map<String,Object> resultMap = createRequestId(user, formid, "0", requestMainMap, detailList);
            out.print(">>>>>测试接口>>>>>resultMap=" + resultMap + "<br/>");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<%!

    private Map<String,Object> createRequestId(User user, String formid, String isnextflow, Map<String, String> mainMap, List<Map<String, Object>> detailList) {
        //主表数据
        ReqOperateRequestEntity requestParam = new ReqOperateRequestEntity();
        List<WorkflowRequestTableField> mainData = new ArrayList<>();
        for (Map.Entry<String, String> entry : mainMap.entrySet()) {
            WorkflowRequestTableField field = new WorkflowRequestTableField();
            field.setFieldName(entry.getKey());
            field.setFieldValue(entry.getValue());
            mainData.add(field);
        }
        requestParam.setMainData(mainData);
        //明细表数据
        List<WorkflowDetailTableInfoEntity> detailData = new ArrayList<>();
        for (Map<String, Object> dtlmap:detailList) {
            String tablename =Util.null2String(dtlmap.get("tablename"));
            List<Map<String, String>> tableFieldList = (List<Map<String, String>>) dtlmap.get("tablefield");
            //设置明细信息
            WorkflowDetailTableInfoEntity detail1 = new WorkflowDetailTableInfoEntity();
            detail1.setTableDBName(tablename);
            //明细数据(多少条明细)
            WorkflowRequestTableRecord[] detailRows = new WorkflowRequestTableRecord[tableFieldList.size()];
            //遍历每一行明细
            int detailRowsIndex = 0;
            for (Map<String, String> map : tableFieldList) {
                //明细行数据（每行有多少个字段）
                WorkflowRequestTableField[] rowDatas = new WorkflowRequestTableField[map.size()];
                int rowDatasIndex = 0;
                //遍历明细每个字段
                for (Map.Entry<String, String> detailMap : map.entrySet()) {
                    //行字段数据 （设置每个字段具体值）
                    WorkflowRequestTableField field1 = new WorkflowRequestTableField();
                    field1.setFieldName(detailMap.getKey());
                    field1.setFieldValue(detailMap.getValue());
                    rowDatas[rowDatasIndex++] = field1;
                }        //封装某行明细数据
                WorkflowRequestTableRecord row1 = new WorkflowRequestTableRecord();
                row1.setRecordOrder(0);
                row1.setWorkflowRequestTableFields(rowDatas);
                detailRows[detailRowsIndex++] = row1;
            }
            //封装所有明细数据
            detail1.setWorkflowRequestTableRecords(detailRows);
            detailData.add(detail1);
        }

        requestParam.setDetailData(detailData);
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("isnextflow", isnextflow);
        requestParam.setOtherParams(otherParams);
        //固定创建流程id
        Map<String, String> wf = getWorkflowId(formid);
        requestParam.setWorkflowId(Util.getIntValue(wf.get("workflowid")));
        requestParam.setRequestName(Util.null2String(wf.get("workflowname")) + "-" + user.getUsername() + "-" + getDateTime());
        WorkflowRequestOperatePA operatePA = ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
        PAResponseEntity paResponseEntity = operatePA.doCreateRequest(user, requestParam);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.putAll((Map<String, Object>) paResponseEntity.getData());
        PAResponseCode paResponseCode = paResponseEntity.getCode();
        resultMap.put("message",paResponseCode.getMessage());
        resultMap.put("statuscode",paResponseCode.getStatusCode()+"");
        ReqFlowFailMsgEntity flowFailMsgEntity = paResponseEntity.getReqFailMsg();
        resultMap.putAll(paResponseEntity.getErrMsg());
        resultMap.putAll(flowFailMsgEntity.getMsgInfo());
        return resultMap;
    }

    private Map<String, String> getWorkflowId(String formid) {
        RecordSet rs = new RecordSet();
        Map<String, String> resultMap = new HashMap<>();
        rs.executeQuery("select (case when activeversionid is null then id else activeversionid end) from workflow_base " +
                "where abs(formid)=? group by (case when activeversionid is null then id else activeversionid end) ", formid);
        rs.next();
        String workflowid = Util.null2String(rs.getString(1));
        resultMap.put("workflowid", workflowid);
        rs.executeQuery("select workflowname from workflow_base where id=?", workflowid);
        rs.next();
        String workflowname = Util.null2String(rs.getString(1));
        resultMap.put("workflowname", workflowname);
        return resultMap;
    }

%>



